package com.zzyl.nursing.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzyl.nursing.dto.ReservationCountDto;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

import com.zzyl.nursing.domain.Reservation;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 预约信息Mapper接口
 *
 * @author ck
 * @date 2025-06-10
 */
@Mapper
public interface ReservationMapper extends BaseMapper<Reservation> {
    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    public Reservation selectReservationById(Long id);

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息集合
     */
    public List<Reservation> selectReservationList(Reservation reservation);

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    public int insertReservation(Reservation reservation);

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    public int updateReservation(Reservation reservation);

    /**
     * 删除预约信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    public int deleteReservationById(Long id);

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteReservationByIds(Long[] ids);

    @Select("select count(*) from reservation where status = 2")
    int cancelCount();

    /**
     * 按时间段统计有效预约次数
     */
    @Select("SELECT DATE_FORMAT(time, '%Y-%m-%d %H:%i:00') AS time, " +
            "COUNT(*) AS count " +
            "FROM reservation " +
            "WHERE time BETWEEN #{start} AND #{end} " +
            "AND status = 0 " + // 只统计待报道状态
            "GROUP BY DATE_FORMAT(time, '%Y-%m-%d %H:%i')")
    List<ReservationCountDto> countReservationsByTimeRange(@Param("start") LocalDateTime start,
                                                           @Param("end") LocalDateTime end);

    /**
     * 检查同一手机号在相同时间段是否有预约
     */
    @Select("SELECT COUNT(*) FROM reservation " +
            "WHERE mobile = #{mobile} " +
            "AND time = #{time}")
    boolean existsByMobileAndTime(String mobile, LocalDateTime time);

    /**
     * 统计指定时间段的有效预约数
     */
    @Select("SELECT COUNT(*) FROM reservation " +
            "WHERE time = #{time} " +
            "AND status = #{status}")
    Integer countByTimeAndStatus(LocalDateTime slotTime, int i);
}
